(ns acst.models.articles
  (:use acst.config.error-config
        acst.utils.handle-exp
        acst.config.success-config)
  (:require [acst.models.db :as db]
            [acst.utils.core :as core]
            [acst.utils.sql-util :as sqlutil]
            [acst.utils.escape :refer [html-sanitizer]]))

(defn find-all-articles [limit page]
  (db/query (:articles-not-found articles-error)
                  ["SELECT article_id,blog_id,username,create_date,title,content
                  FROM users,articles WHERE publisher_id = user_id
                  ORDER BY create_date LIMIT ? OFFSET ?" limit (* (- page 1) limit)]))

(defn find-articles
  [where-cluse limit page]
  (->> (as-> (condp = (keys where-cluse)
               '(:blog_id)
               ["blog_id = ?" (where-cluse :blog_id)]
               '(:article_id)
               ["article_id = ?" (where-cluse :article_id)]
               '(:keyword)
               ["title = ? OR content = ?" (where-cluse :keyword) (where-cluse :keyword)]
               '(:blog_id :keyword)
               ["blog_id = ? AND (title = ? OR content = ?)" (where-cluse :blog_id) (where-cluse :keyword) (where-cluse :keyword)]) $
             (core/conj-all [(str "SELECT article_id, blog_id, create_date, title, content FROM articles WHERE "
                                  (first $) " LIMIT ? OFFSET ?")]
                            (rest $) [limit (* (- page 1) limit)]))
       (db/query (:articles-not-found articles-error))))

(defn insert-article! [user_id title content]
  (as-> (db/query (:blog-not-active blogs-error) ["SELECT blog_id FROM blogs WHERE owner_id = ?" user_id]) $ ;;获取用户对应的blog_id
        (cond (get-error $)
              $                                             ;;如果有错误返回错误信息
              (:blog_id (first $))
              (db/insert! :articles                         ;;否则将文章插入到对应文章
                          {:blog_id (:blog_id (first $))
                           :publisher_id user_id
                           :title (html-sanitizer title)
                           :content (html-sanitizer content)}))))

(defn update-article! [user_id article_id title content]
  (as-> (db/query (:blog-not-active blogs-error) ["SELECT blog_id FROM blogs WHERE owner_id = ?" user_id]) $
        (cond (get-error $) $
              (:blog_id (first $))
              (sqlutil/handle-update!
                user_id
                :articles
                {:blog_id (:blog_id (first $))
                 :title (html-sanitizer title)
                 :content (html-sanitizer content)}
                ["article_id = ? AND blog_id = ?" article_id (:blog_id (first $))]
                ["article_id = ? AND blog_id = ?" article_id (:blog_id (first $))]
                (:update-article-success articles-result)
                (:update-article-failed articles-error)))))

(defn delete-article! [article_id user_id]
  (sqlutil/handle-delete!
    user_id
    :articles
    ["article_id = ? AND publisher_id = ?" article_id user_id]
    ["article_id = ?" article_id]
    (:delete-article-success articles-result)
    (:delete-article-failed articles-error)))